<?php



include '../include/init.php';


$type=$_POST['t'];


//如果是获取用户验证信息的话
if($type=='getval')
{
	$fid=$_POST['f'];
	$cfg=new configModel();
	$ineed=$_POST['i'];
	if(strpos($ineed,',')!==false)
	{
		$ineed=explode(',',$ineed);
		$msg=$cfg->gets($fid,$ineed);
	}
	else
	{
		$msg=$cfg->get($fid,$ineed);
	}

	if($msg)
	{
		echo json_encode($msg);
	}
	else
	{
		echo json_encode('no');
	}
}//如果是发送添加好友信息
else if($type=='add')
{
	$fid=$_POST['f'];
	$uid=$_POST['u'];
	
	$flag=trim($_POST['flag']);//是否直接添加其为好友，空则添加，非空则不直接添加


	//判断发送验证者与被添加用户的关系，如果是自己或已经是好友，则返回
	$fm=new frdModel();
	$isfrd=$fm->bothfrd($uid,$fid);
	if($isfrd==1||$isfrd==2)
	{
		die(json_encode('no'));
	}
	$data=array();

	if($flag=="")//是否直接添加其为好友，空则添加，非空则不直接添加
	{
		$cfg=new configModel();
		$addtype=$cfg->get($fid,'addvalidate');
	
		$currentanswer=$cfg->get($fid,'addvalidate_answer');
		$answer=trim($_POST['val']);
		
		//再次判断答案是否正确再添加
		if(($addtype==1)||($addtype==3&&$currentanswer==$answer))
		{
			/*
			$dt=array();
			$dt['frdname']=$_POST['bak'];
			$dt['gid']=$_POST['tg'];
			$dt['acc_to_space']=$_POST['aon'];
			$dt['frdid']=$fid;
			$fm->add($uid,$dt);
			*/
			$data['isadd']=1;
		}
	}



	
	$data['ownid']=$uid;
	$data['toid']=$fid;
	$data['validate_str']=$_POST['val'];
	$data['bakname']=$_POST['bak'];
	$data['togrp']=$_POST['tg'];
	$data['acc_to_space']=$_POST['aon'];
	$data['isread']=0;
	$ad=new addfrdModel();
	//查看该用户是否已经有未处理isadd=2验证消息且ignore_this=0，如果有则将之前未处理且未忽略的验证消息update为本条
	$hasid=$ad->hasverify($uid,$fid);
	if($hasid)
	{
		$uo=$ad->updateone($hasid,$data);
		if($uo!==false)
		{
			die(json_encode('ok'));
		}
		else
		{
			die(json_encode('no'));
		}
	}
	else
	{	
		if($ad->add($data))
		{
			die(json_encode('ok'));
		}
		else
		{
			die(json_encode('no'));
		}
	}
}
else if($type=="addgrp")
{
	$uid=$_POST['u'];
	$grpname=$_POST['g'];

	$gm=new groupModel();

	$rs=$gm->add($uid,$grpname);
	if($rs)
	{
		echo json_encode($rs);
	}
	else
	{
		echo json_encode('no');
	}
}
else if($type=='judge')
{
	$fid=$_POST['f'];
	$answer=trim($_POST['an']);
	$cfg=new configModel();
	$currentanswer=$cfg->get($fid,'addvalidate_answer');
	
	if($currentanswer==$answer)
	{
		echo json_encode('ok');
	}
	else
	{
		echo json_encode('no');
	}

}
